package com.spark.controller;

import com.spark.Service.DiaryService;
import com.spark.pojo.Diary;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import com.spark.pojo.Msg;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/diaries")
public class DiaryController {
    @Autowired
    DiaryService diaryService;

    @ResponseBody
    @RequestMapping("/all/{userId}")
    public Msg getAllByUserId(@PathVariable("userId") String id) {

        List<Diary> diaries = diaryService.selectAllByUserId(id);
        return Msg.success().addAttribute("diaries", diaries);
    }

    @ResponseBody
    @RequestMapping(value = "/search", method = RequestMethod.POST)
    public Msg search(Diary diary, @RequestParam(value = "date", defaultValue = "0-0-0") String date) {
        List<Diary> diaries = diaryService.selectByConditions(diary, date);
        return Msg.success().addAttribute("diaries", diaries);
    }

    @ResponseBody
    @RequestMapping("/years/{userId}")
    public Msg getYears(@PathVariable("userId") String id) {
        List<Integer> years = diaryService.getYearsByUserId(id);
        return Msg.success().addAttribute("years", years);
    }


    /**
     * @Author workhere
     * @Description 根据diaryID得到日记信息
     * @version: 1.0
     * @Date  2021/8/11 16:20
     * @Param String diaryID 需要获取的日记的ID
     **/
    @ResponseBody
    @RequestMapping("/{diaryId}")
    public Msg getDiary(@PathVariable("diaryId") String diaryId) {
        Diary diary = diaryService.getDiaryByDiaryId(diaryId);
//        System.out.println(diaryId);
        return Msg.success().addAttribute("diary", diary);
    }
}
